<template>
    <div class="login-container" :style="{ backgroundImage: `url(${bgUrl})` }">
        <div class="login-content">
            <div class="login-content-left">
                <div class="login-content-left-wrap">
                    <h1>欢迎登录</h1>
                    <span>春木亦后台</span>
                </div>
            </div>
            <div class="login-content-right">
                <el-form 
                    ref="ruleFormRef"
                    :model="loginForm" 
                    :rules="rules" 
                    label-position="top" 
                    size="large"
                    status-icon>
                    <el-form-item prop="username">
                        <template #label>
                            <span style="font-weight: bold">账号</span>
                        </template>
                        <el-input v-model="loginForm.username" placeholder="请输入账号">
                            <template #prefix>
                                <i class="iconfont icon-zhanghao" />
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <template #label>
                            <span style="font-weight: bold">密码</span>
                        </template>
                        <el-input v-model="loginForm.password" type="password" show-password placeholder="请输入密码">
                            <template #prefix>
                                <i class="iconfont icon-password" />
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" style="width: 100%" @click="loginFn">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script setup lang='ts'>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { FormInstance, FormRules, ElMessage } from 'element-plus'
import { useUser } from '@/stores/app'
import { isDark } from '@/utils/dark'

let rightBgColor = ref<string>()
const router = useRouter()
const userStores = useUser()
const bgUrl = ref('https://img.tukuppt.com/bg_grid/00/03/96/5ar0sl6nJU.jpg!/fh/350')
const ruleFormRef = ref<FormInstance>()
const loginForm = reactive({
    username: '',
    password: ''
})
const uNameValidate = (rule:any, value:string, callback:any) => {
    if(!value) {
        return callback('账号不能为空')
    } else if (value.length < 5 || value.length > 16) {
        return callback('账号长度为5-16位') 
    } else if (/[^A-z\d_]/.test(value)) {
        return callback('账号由数字、大小写字母、下划线组成')
    } else {
        callback()
    }
}
const psdValidate = (rule:any, value:string, callback:any) => {
    let reg1 = '\\d', reg21 = '[a-z]', reg22 = '[A-Z]', reg3 = '\\W'
    let reg = new RegExp(`^(${reg1}|${reg21}|${reg22}|${reg3})`)
    if(!value) {
        return callback('密码不能为空')
    } else if (value.length < 5 || value.length > 16) {
        return callback('密码长度为5-16位') 
    } else if (!reg.test(value)) {
        return callback('密码由数字、大小写字母、特殊字符组成')
    } else {
        callback()
    }
}
const rules = ref<FormRules>({
    username: [
        { validator: uNameValidate, trigger: 'blur' }
    ],
    password: [
        { validator: psdValidate, trigger: 'blur' }
    ]
})

onMounted(() => {
    if(isDark.value) {
        rightBgColor.value = '#000000ee'
    } else {
        rightBgColor.value = '#ffffff'
    }
})

// 登录业务
const loginFn = () => {
    ruleFormRef.value?.validate(valid => {
        if (valid) {
            userStores.login(loginForm).then(res => {
                ElMessage.success({
                    message: '登录成功'
                })
                router.replace('/home')
            })
        }
    })
    // router.push('/home')
}
</script>
<style lang="scss" scoped>
.login-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background-repeat: no-repeat;
    background-size: 100% 100%;

    .login-content {
        width: 55%;
        height: 50%;
        border-radius: 10px;
        overflow: hidden;
        display: flex;

        &-left {
            flex: 0.5;
            background-color: rgb(15, 61, 99);
            opacity: 0.97;
            color: #fff;
            position: relative;

            &-wrap {
                position: absolute;
                top: 49%;
                left: 3%;
                transform: translateY(-50%);
            }
        }

        &-right {
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: v-bind(rightBgColor);
            flex: 0.5;

            .el-form {
                width: 70%;
            }
        }
    }
}
</style>